package com.cf.r1031_d;

import java.io.*;
import java.util.StringTokenizer;

public class c {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        PrintWriter out = new PrintWriter(System.out);
        int t = Integer.parseInt(br.readLine());
        while (t-- > 0) {
            String[] ss = br.readLine().split(" ");
            int n = Integer.parseInt(ss[0]);
            int m = Integer.parseInt(ss[1]);
            int k = Integer.parseInt(ss[2]);
            String[][] arr = new String[n][m];
            for (int i = 0; i < n; i++) {
                String[] s = br.readLine().split("");
                System.arraycopy(s, 0, arr[i], 0, m);
            }
            int[][] f = new int[n + 1][m + 1];
            for (int i = 0; i < n; i++) {
                for (int j = 0; j < m; j++) {
                    f[i + 1][j + 1] = arr[i][j].equals("g") ? 1 : 0;
                    // 二维前缀和
                    f[i + 1][j + 1] += f[i + 1][j] + f[i][j + 1] - f[i][j];
                }
            }
            int ans = n * m;
            for (int i = 0; i < n; i++) {
                for (int j = 0; j < m; j++) {
                    if (arr[i][j].equals(".")) {
                        int x1 = Math.max(0, i - k + 1), x2 = Math.min(n - 1, i + k - 1);
                        int y1 = Math.max(0, j - k + 1), y2 = Math.min(m - 1, j + k - 1);
                        ans = Math.min(ans, f[x2 + 1][y2 + 1] - f[x1][y2 + 1] - f[x2 + 1][y1] + f[x1][y1]);
                    }
                }
            }
            out.println(f[n][m] - ans);
        }
        out.close();
        out.close();
        br.close();
    }

}
